package com.talent.web;

import com.talent.page.Page;
import com.talent.pojo.Depart;
import com.talent.service.DepartService;
import com.talent.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;



/**
 * 院系
 */
@Controller
@RequestMapping("/manage/depart")
public class DepartController extends BaseController{

    @Autowired
    DepartService departService;


    @RequestMapping(value = "/manager",method = RequestMethod.GET)
    public ModelAndView departUI(Depart depart, Integer currentPage, ModelAndView mav){
        try {
            Page<Depart> page = departService.selectDepartPage(depart, currentPage == null ? 1 : currentPage);
            // 列表项
            mav.addObject("page", page.getResultList());
            // 分页参数
            initPageParams(mav, page);
            // 搜索条件
            mav.addObject("depart", depart);
            mav.setViewName("depart/manager");
        }catch (Exception e){
            mav.setViewName("error/500");
        }
        return mav;
    }


    /**
     * @Title: addUI
     * @Description: 去添加页面
     * @return
     */
    @RequestMapping(value = "/addUI",method = RequestMethod.GET)
    public Object addUI() {
       return "depart/addUI";
    }


    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Result DepartAdd(Depart depart) {
        try {
            departService.addDepart(depart);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    /**
     * @Title: addUI
     * @Description: 去修改页面
     * @return
     */
    @RequestMapping("editUI")
    public ModelAndView editUI(Integer id) {
        ModelAndView mav = new ModelAndView();
        try {
            Depart depart = departService.selectById(id);
            mav.addObject("depart",depart);
            mav.setViewName("depart/editUI");
        } catch (Exception e) {
            mav.setViewName("/error/500");
        }
        return mav;
    }


    /**
     * @Title: edit
     * @Description: 修改
     * @return
     */
    @RequestMapping(value = "/edit111", method = RequestMethod.POST)
    @ResponseBody
    public Object edit(@RequestBody Depart depart) {
        return departService.editDepart(depart);
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public Result classUpdate(Depart depart) {

        try {
            departService.editDepart(depart);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Object deleteIDs(@PathVariable Integer id) {
       return departService.deleteById(id);
    }


    /**
     * 校验
     * @param name
     * @param code
     * @param abbr
     * @return
     */
    @RequestMapping(value = "/validate",method = RequestMethod.GET)
    @ResponseBody
    public Object validate(@RequestParam("name") String name, @RequestParam("code") String code,@RequestParam("abbr") String abbr){
        int flag = 0;
        try {
            flag = departService.validate(name, code, abbr);
            if(flag > 0){
                return renderError(name + code + abbr+"已存在");
            }
            return renderSuccess("");
        }catch (Exception e){
            e.printStackTrace();
            return renderError("系统错误!");
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/departDeleteIDs",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteAll(String ids) {
        try {
            departService.deleteAll(ids.split(","));
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    @RequestMapping("departDetail")
    public ModelAndView departDetail(Integer id) {
        ModelAndView mav = new ModelAndView();
        try {
            Depart depart = departService.selectById(id);
            mav.addObject("depart", depart);
            mav.setViewName("depart/detail");
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }
}
